package com.jxfgyj.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.jxfgyj.dao.TeacherDao;
import com.jxfgyj.domain.DO.jxfgyj_teacher;

public class TeacherDaoImpl implements TeacherDao {
	private SessionFactory sessionFactory;

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		return this.sessionFactory.getCurrentSession();
	}

	/*
	 * 
	 */
	@Override
	public jxfgyj_teacher getTeacher_byID(String video_teacher) {
		String hql = " from jxfgyj_teacher  where teacher_id='" + video_teacher + "'";
		Query query = getSession().createQuery(hql);
		List<jxfgyj_teacher> list = query.list();
		getSession().clear();
		if (list.size() == 0) {
			return null;
		} else {
			return list.get(0);
		}

	}

	@Override
	public boolean createTeacher(jxfgyj_teacher teacher) {
		Session session = getSession();
		session.save(teacher);
		session.flush();
		return true;
	}

	@Override
	public List<jxfgyj_teacher> listTeacher(String search) {
		Session session = getSession();
		String hql = "from jxfgyj_teacher "//
				+ " where teacher_name like :search " //
				+ " order by teacher_name desc";
		//
		Query query = session.createQuery(hql);
		//
		if (null == search || search.equals("")) {
			query.setParameter("search", "%%");
		} else {
			query.setParameter("search", "%" + search + "%");
		}
		//
		List<jxfgyj_teacher> workList = query.list();
		session.clear();
		//
		return workList;
	}

	@Override
	public void deleteTeacher_byID(String teacher_id) {
		Session session = getSession();
		String hql = "delete from jxfgyj_teacher where teacher_id=:teacher_id";
		Query query = session.createQuery(hql);
		//
		query.setParameter("teacher_id", teacher_id);
		//
		query.executeUpdate();
		session.flush();

	}
	/*
	 * 
	 */

}
